#include <g2d.h>

g2d::Result create_world(g2d::Game2D &g2d) {
  auto scene = g2d.load_scene("menu");
  if (!scene) return g2d::Result::E_Fail;

  auto layer = scene->create_layer("0");
  if (!layer) return g2d::Result::E_Fail;

  auto ui = dynamic_cast<g2d::ctrl::UserInterface*>(layer->create_gobj("ui"));
  if (!ui) return g2d::Result::E_Fail;

  auto ver = dynamic_cast<g2d::ctrl::VerLayout*>(ui->create_gobj("verlayout"));
  if (!ver) return g2d::Result::E_Fail;
  ui->set_root(ver);
  ver->set_attribute("align", "center");
  ver->set_attribute("padding", "20");
  ver->set_attribute("bkcolor", "gray");

  ver->create_gobj("control");

  static const char * texts[4] = {
    "PLAY", "CONTINUE", "SETTING", "EXIT"
  };
  for (int i = 0; i < 4; ++i) {
    auto btn = dynamic_cast<g2d::ctrl::Button*>(ver->create_gobj("button"));
    if (!btn) return g2d::Result::E_Fail;
    btn->set_text(texts[i]);
    btn->set_font("font");
    btn->set_attribute("textcolor", "#ffa924");
    btn->set_attribute("hottextcolor", "#f17f05");
    btn->set_attribute("pushedtextcolor", "#df4c0b");
    btn->set_attribute("disabledtextcolor", "#777");
  }

  ver->create_gobj("control");

  auto rst = g2d.font().load("font", "fonts/simfang.ttf", 40);
  if (g2d::Succeeded(rst))
    rst = g2d.change_scene(scene);
  return rst;
}

int main(int argc, char *argv[]) {
  Uint32 fps = 60;

  g2d::Game2D g2d;
  auto rst = g2d.init("Game2D g2d", 650, 468);
  
  if (g2d::Succeeded(rst))
    rst = create_world(g2d);
  
  if (g2d::Succeeded(rst)) {
    g2d.set_fps(fps);
    g2d.run();
  }

  g2d.uninit();
  return 0;
}